﻿using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.SerivesPlugin.Ronma
{
    [Description("受托加工材料退料，自动提交"), HotUpdate]
    public class RonmaAutoOperation : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("F_IsAutoAudit");
        }
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);
            var ids = (from w in e.DataEntitys
                       where Convert.ToBoolean(w["F_IsAutoAudit"])
                       select w into s
                       select s["id"]).ToArray();
            if (ids.Count() <= 0) { return; }
            if (this.Context.ServiceType != WebType.WebService) { return; }
            IOperationResult operationResult = BusinessDataServiceHelper.Submit(this.Context, this.BusinessInfo, ids, "Submit", OperateOption.Create());
            string dStatus = e.DataEntitys[0]["DocumentStatus"].ToString();
            string billNo = e.DataEntitys[0]["BillNo"].ToString();
            if (CheckOpResult(operationResult, dStatus, billNo)) { }
        }

        private bool CheckOpResult(IOperationResult operationResult, string dStatus, string billNo)
        {
            if (operationResult.IsSuccess)
            {
                return true;
            }
            if (operationResult.OperateResult == null)
            {
                throw new KDBusinessException("AutoOperate-001", "未知原因导致自动提交失败！");
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("自动提交失败，失败原因：");
            foreach (var opRes in operationResult.OperateResult)
            {
                stringBuilder.AppendLine(opRes.Message);
            }
            if (operationResult.InteractionContext != null && !operationResult.InteractionContext.SimpleMessage.IsNullOrEmptyOrWhiteSpace())
            {
                stringBuilder.AppendLine(operationResult.InteractionContext.SimpleMessage);
            }
            stringBuilder.AppendLine(GetStatus(dStatus, billNo));
            throw new KDBusinessException("AutoOperate-002", stringBuilder.ToString());
        }

        private string GetStatus(string dStatus, string billNo)
        {
            string status = "";
            if (dStatus.ToUpper() == "C")
            {
                status = $"ERP单号：{billNo},已经审核！不允许重复提交！";
            }
            if (dStatus.ToUpper() == "B")
            {
                status = $"ERP单号：{billNo},已经提交！不用重复提交！";
            }
            return status;
        }
    }
}
